package com.ruoyi.basic.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.ITreeNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;


/**
 * @author zyxis
 */
@TableName("tb_category_real")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class TbCategoryReal extends BaseEntity implements ITreeNode<Long, Boolean, Long>
{
    private static final long serialVersionUID = 1L;

    /** id */
    @TableId(value = "id",type = IdType.AUTO)
    @TableField("`id`")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    /** 父分类id */
    @Excel(name = "父分类id")
    @TableField("`parent_id`")
    private Long parentId;

    /** 父分类名称 */
    @Excel(name = "父分类名称")
    @TableField(exist = false)
    private String parentName;

    /** 祖级列表 */
    @Excel(name = "祖级列表")
    @TableField("`ancestors`")
    private String ancestors;

    /** 分类名称 */
    @Excel(name = "分类名称")
    @TableField("`name`")
    @NotBlank(message = "分类名称不能为空")
    @Size(max = 32,message = "分类名称长度最多32个字符")
    private String name;

    /** 分类编码 */
    @Excel(name = "分类编码")
    @TableField("`code`")
    private String code;

    /** 级别 */
    @Excel(name = "级别")
    @TableField("`level`")
    private Integer level;

    /** 显示顺序 */
    @Excel(name = "显示顺序")
    @TableField("`order_num`")
    @NotNull(message = "显示顺序不能为空")
    private Long orderNum;

    /** 状态（0正常 1停用） */
    @Excel(name = "状态", readConverterExp = "0=正常,1=停用")
    @TableField("`status`")
    private String status;

    /** 删除标志（0代表存在 1代表删除） */
    @TableField("`del_flag`")
    private String delFlag;

    /** 排除编号 */
    @TableField(exist = false)
    private Long excludeId;

    @TableField(exist = false)
    private BigDecimal maxPrice = BigDecimal.ZERO;

    @TableField(exist = false)
    private BigDecimal minPrice = BigDecimal.ZERO;

    @TableField(exist = false)
    private BigDecimal avgPrice = BigDecimal.ZERO;

    @TableField(exist = false)
    private Long unitPriceId;

    @TableField(exist = false)
    private Long detailId;

    @TableField(exist = false)
    private List<TbCategoryReal> children;

    @Override
    public Long getTreeNodeId() {
        return this.id;
    }

    @Override
    public boolean isRoot() {
        return Objects.equals(this.parentId, 100L);
    }

    @Override
    public boolean isRoot(Boolean rootCondition) {
        // 自定义的父节点判定规则
        if (rootCondition){
            return Objects.equals(this.id, 100L);

        } else {
            // 都不符合就走默认判定条件
            return isRoot();
        }
    }

    @Override
    public boolean isChildren(Long leafCondition) {
        // 自定义结点判定规则
        // 这里自定义规则当传入的参数等于1的时候 ——>
        if (leafCondition.equals(1)){
            /*if (this.name.contains("")){
                return true;
            } else {
                return false;
            }*/
        } else {
            // 都不符合就表示该节点不是自定义规则中要的结点
            // return false;
        }
        return false;
    }

    @Override
    public boolean hasChild() {
        return !Objects.isNull(this.children);
    }

    @Override
    public void setChildren(List children) {
        this.children = children;
    }

    @Override
    public List<TbCategoryReal> getChildren(){
        return this.children;
    }
}
